<?php if ( !defined('BASEPATH')) exit('No direct script access allowed');

class Charge_list extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');

	}
	public function index(){
	  $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  $this->load->view($this->session->userdata('formain'));
	  $this->load->view('layouts/footer');
	 }

	 public function insert_charge_list(){
	 	$data = array(
	 		':p_list_name'		=> $this->input->post('list_name'),
			':p_description'	=> $this->input->post('list_desc'),
			':p_user_id'		=> $this->session->userdata('user_id')
	 	);

	 	$this->charge_list_model->insert_chg_list($data);
	 }

	 public function update_charge_list(){
	 	$data = array(
	 		':p_list_name'		=> $this->input->post('edit_list_name'),
			':p_description'	=> $this->input->post('edit_list_desc'),
			':p_list_id'		=> $this->input->post('edit_list_id'),
			':p_user_id'		=> $this->session->userdata('user_id')
	 	);

	 	$this->charge_list_model->update_chg_list($data);
	 }

	 public function search_charge_list(){
		$list_name = $this->input->post('s_list_name');
	 	$query = $this->charge_list_model->search_chg_list($list_name);
	 	if(!empty($query)) {
			echo json_encode($query);
		}else{
			echo json_encode("none");
		}
	 }

	 public function insert_charge_list_seq(){
	 	$chg_code = $this->charge_list_model->get_chg_code($this->input->post('list_code'));
	 	$data = array(
	 		':p_list_id'		=> $this->input->post('c_list_id'),
	 		':p_seq_num'		=> $this->input->post('seq_num'),
	 		':p_charge_code'	=> $chg_code[0]->charge_code,
	 		':p_option_code'	=> $this->input->post('list_opt'),
	 		':p_user_id'		=> $this->session->userdata('user_id')
	 		);

	 	$query = $this->charge_list_model->insert_chg_list_line($data);
		if($query[0]->error == 1){
			echo json_encode($query);
		}else{
			$this->view_sequence($this->input->post('c_list_id'));
		}
	 }
	 public function delete_charge_list_seq(){
	 	$data = array(
	 		':p_line_id'		=> $this->input->post('line-id')
	 		);
	 	$query = $this->charge_list_model->delete_chg_list_line($data);
		if($query[0]->error == 1){
			echo json_encode($query);
		}else{
			$this->view_sequence($this->input->post('list-id'));
		}
	 }
	public function update_sequence(){
		$data = array(
			':p_line_id'	=> $this->input->post('line-id'),
			':p_seq_num'	=> $this->input->post('seq_num'),
			':p_user_id'	=> $this->session->userdata('user_id')
		);
		$this->charge_list_model->update_seq($data);
	} 
	public function view_sequence($id){
		$query = $this->charge_list_model->get_sequence($id);
		echo json_encode($query);
	}

	
	public function search_option(){
		$query = $this->charge_list_model->get_options($this->input->post('chg_elem_id'));
		echo json_encode($query);

	}
	public function search_opt_val(){
		$query = $this->charge_list_model->get_option_value($this->input->post('opt_elem_code'));
		echo json_encode($query);

	}


	public function add_form(){
		$this->load->view($this->session->userdata('foradd'),array(
			'elements'		=> $this->charge_list_model->get_charges()
		));
	}
	public function edit_form($id){
		$this->load->view($this->session->userdata('foredit'),array(
			'elements'		=> $this->charge_list_model->get_charges(),
			'list_data'		=> $this->charge_list_model->get_chg_list($id)		
		));
	}
	public function view_form($id){
		$this->load->view($this->session->userdata('forview'),array(
			'list_data'			=> $this->charge_list_model->get_chg_list($id),
			'list_seq_data'		=> $this->charge_list_model->get_sequence($id)	
		));
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */